﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.VisualBasic;
using System.Linq;

using SerialPortAssistant;

namespace ATui
{
    public partial class MainForm : Form
    {
        // declare sendATcmd class
        sendATcmd atCmd = new sendATcmd();

        // declare ATdb dataContext
        ATdbDataContext ATdbData = null;

        #region MainForm initialise
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                // Add all coms name to comboBox
                Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
                foreach (string s in pc.Ports.SerialPortNames)
                {
                    this.comboBox_serialPortName.Items.Add(s);
                }

                // create ATdb dataContext
                ATdbData = new ATdbDataContext();

                // add numButton control input event delegate instance
                numButton2.ItemEventHandle += new MIS.Component.ItemEvent(this.NumInput);
                numButton2.ItemDialEventHandle += new MIS.Component.DoWorkEventHandler(this.NumDial);
                numButton2.ItemCancleEventHandle += new MIS.Component.DoWorkEventHandler(this.NumCancle);

                //如果有配置 则读取配置文件
                if (File.Exists(Application.StartupPath + @"\SerialPortConfig.wjs"))
                {
                    using (StreamReader sr = new StreamReader(Application.StartupPath + @"\SerialPortConfig.wjs", Encoding.UTF8))
                    {
                        this.comboBox_serialPortName.SelectedItem = sr.ReadLine();
                        this.comboBox_serialPort_BaudRate.SelectedItem = sr.ReadLine();
                        this.comboBox_serialPort_parity.SelectedItem = sr.ReadLine();
                        this.comboBox_serialPort_DataBits.SelectedItem = sr.ReadLine();
                        this.comboBox_serialPort_stopBit.SelectedItem = sr.ReadLine();
                    }

                    // create a Serial Port object
                    atCmd.sp = new System.IO.Ports.SerialPort(comboBox_serialPortName.SelectedItem.ToString());

                    //open serial port 
                    atCmd.sp.Open();
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        # region 自定义AT指令page
        private void button_Action_Click(object sender, EventArgs e)
        {
            List<string> cmdList = atCmd.praseTextCmd(textBox_AT_input.Text);

            foreach (string cmd in cmdList)
            {
                textBox_AT_output.Text += atCmd.sendSerialAtCmd(cmd, checkbox_hex.Checked);
            }
        }

        private void button_AT_clear_Click(object sender, EventArgs e)
        {
            textBox_AT_output.Clear();
        }

        private void checkBox_ASC_Click(object sender, EventArgs e)
        {
            if (checkBox_ASC.Checked == true)
            {
                checkbox_hex.Checked = false;
            }
            else
            {
                checkbox_hex.Checked = true;
            }
        }

        private void checkbox_hex_Click(object sender, EventArgs e)
        {
            if (checkbox_hex.Checked == true)
            {
                checkBox_ASC.Checked = false;
            }
            else
            {
                checkBox_ASC.Checked = true;
            }
        }
        # endregion

        # region 串口page
        private void button_saveConfig_Click(object sender, EventArgs e)
        {
            if (atCmd.sp.IsOpen)
            {
                using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\SerialPortConfig.wjs", false, Encoding.UTF8))
                {
                    sw.WriteLine(comboBox_serialPortName.SelectedItem.ToString());
                    sw.WriteLine(comboBox_serialPort_BaudRate.SelectedItem.ToString());
                    sw.WriteLine(comboBox_serialPort_parity.SelectedItem.ToString());
                    sw.WriteLine(comboBox_serialPort_DataBits.SelectedItem.ToString());
                    sw.WriteLine(comboBox_serialPort_stopBit.SelectedItem.ToString());
                    sw.Flush();
                }
                MessageBox.Show("配置已成功保存 下次程序启动后会自动读取配置");
            }
            else
            {
                MessageBox.Show("只有成功打开了串口 才能保存配置");
            }
        }

        private void button_select_com_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox_serialPortName.SelectedItem != null)
                {
                    if (atCmd.sp == null)
                    {
                        // create a Serial Port object
                        atCmd.sp = new System.IO.Ports.SerialPort(comboBox_serialPortName.SelectedItem.ToString());
                    }
                    else
                    {
                        if (atCmd.sp.IsOpen == true)
                        {
                            // close serial port
                            atCmd.sp.Close();
                        }

                        // set 串口号
                        atCmd.sp.PortName = comboBox_serialPortName.SelectedItem.ToString();
                    }

                    // set 波特率
                    atCmd.sp.BaudRate = Convert.ToInt32(this.comboBox_serialPort_BaudRate.SelectedItem.ToString());

                    // set 数据位
                    atCmd.sp.DataBits = Convert.ToInt32(this.comboBox_serialPort_DataBits.SelectedItem.ToString());

                    // set 检验位
                    switch (this.comboBox_serialPort_parity.SelectedIndex)
                    {
                        case 0:
                            atCmd.sp.Parity = Parity.None;
                            break;
                        case 1:
                            atCmd.sp.Parity = Parity.Odd;
                            break;
                        case 2:
                            atCmd.sp.Parity = Parity.Even;
                            break;
                        case 3:
                            atCmd.sp.Parity = Parity.Mark;
                            break;
                        case 4:
                            atCmd.sp.Parity = Parity.Space;
                            break;
                    }

                    // set 停止位
                    switch (this.comboBox_serialPort_stopBit.SelectedIndex)
                    {
                        case 0:
                            atCmd.sp.StopBits = StopBits.One;
                            break;
                        case 1:
                            atCmd.sp.StopBits = StopBits.Two;
                            break;
                        case 2:
                            atCmd.sp.StopBits = StopBits.OnePointFive;
                            break;
                    }

                    try
                    {
                        if (atCmd.sp.IsOpen != true)
                        {
                            //open serial port 
                            atCmd.sp.Open();
                            MessageBox.Show(comboBox_serialPortName.SelectedItem.ToString() + "打开成功！");
                        }
                        else
                        {
                            MessageBox.Show(comboBox_serialPortName.SelectedItem.ToString() + "已打开！");
                        }



                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button_debug_Click(object sender, EventArgs e)
        {
            FrmMain debug = new FrmMain();
            debug.Show();
        }
        # endregion

        #region 手机拨号
        private void button_phone_powerup_Click(object sender, EventArgs e)
        {
            textBox_phone_Num_feedback.Text += atCmd.sendSerialAtCmd("at+cfun=1", false);
        }

        private void button_power_off_Click(object sender, EventArgs e)
        {
            textBox_phone_Num_feedback.Text += atCmd.sendSerialAtCmd("at+cfun=0", false);
        }

        private void button_phoneDial_clear_Click(object sender, EventArgs e)
        {
            textBox_phone_Num_feedback.Clear();
        }

        #region  numButton control delegate function instance

        public void NumInput(string num)
        {
            textBox_phone_Num.Text += num;
        }

        public void NumDial(object sender, EventArgs e)
        {
            textBox_phone_Num_feedback.Text += atCmd.sendSerialAtCmd("ATD" + textBox_phone_Num.Text.Trim() + ";", false);
        }

        public void NumCancle(object sender, EventArgs e)
        {
            textBox_phone_Num_feedback.Text += atCmd.sendSerialAtCmd("ATH", false);
        }

        #endregion


        # endregion

        
        


    }
}
